package com.fg.pro.ai.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cloud.ai.dashscope.image.DashScopeImageModel;
import com.alibaba.cloud.ai.dashscope.image.DashScopeImageOptions;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fg.pro.ai.dao.AiImageDao;
import com.fg.pro.ai.entity.AiImage;
import com.fg.pro.ai.service.AiImageService;
import com.fg.pro.model.reponse.ResponseResult;
import com.fg.pro.model.utils.UserIdThread;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * ai图片(AiImage)表服务实现类
 *
 * @author makejava
 * @since 2025-09-12 15:36:54
 */
@Service("aiImageService")
public class AiImageServiceImpl extends ServiceImpl<AiImageDao, AiImage> implements AiImageService {
    @Autowired
    private DashScopeImageModel dashScopeImageModel;
    @Override
    public boolean saveByAi(String imgDes,String modelCode) throws InterruptedException {
        DashScopeImageOptions imageOptions = DashScopeImageOptions
                .builder()
                .withModel(modelCode)
                .build();
        ImagePrompt imagePrompt = new ImagePrompt(imgDes, imageOptions);
        // todo spring-ai-alibaba-starter-dashscope 1.0.0.2  目前1.0.0.3 有BUG，等以后看看版本会不会更新
        ImageResponse response = dashScopeImageModel.call(imagePrompt);

//        while (true){
//            if(response.getResult()!=null){
//                break;
//            }
//            System.out.println("生成中。。。。。。。。。。。");
//            TimeUnit.SECONDS.sleep(3);
//        }

        // 图片url
        String url = response.getResult().getOutput().getUrl();
        // 图片base64
        String b64Json = response.getResult().getOutput().getB64Json();

        AiImage aiImage = new AiImage();
        aiImage.setImageDes(imgDes);
        aiImage.setImageBase64Json(b64Json);
        aiImage.setImageUrl(url);
        aiImage.setImageDownloadUrl(url);
        aiImage.setImageAiModel(modelCode);
        if(UserIdThread.sysUserId()!=null){
            aiImage.setCreateUserId(UserIdThread.sysUserId());
        }else {
            aiImage.setCreateUserId(Long.valueOf("12"));
        }
        boolean save = save(aiImage);
        return save;
    }

    @Override
    public PageInfo getPage(Integer pageNum, Integer pageSize, Date start, Date end, String imgDes) {
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        PageHelper.startPage(pageNum, pageSize);
        List<AiImage> list = query()
                .like(StrUtil.isNotBlank(imgDes), "image_des", imgDes)
                .ge("create_time", sdfs.format(start)).le("create_time", sdfs.format(end)).orderByDesc("create_time")
                .list();
        PageInfo<AiImage> pageInfo= new PageInfo(list);
        return pageInfo;
    }

}

